package jy.game.tools;

import android.text.TextUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import jy.game.BasePojo;
import jy.game.UserDefinedAnimationsData;
import jy.game.components.battle.JyBattle;
import jy.game.components.event.triggers.JyGlobalTrigger;
import jy.game.components.item.JyItem;
import jy.game.components.map.JyMap;
import jy.game.components.menpai.JyMenpai;
import jy.game.components.mod.JyModInfo;
import jy.game.components.music.JyMusics;
import jy.game.components.resource.JyResource;
import jy.game.components.role.JyRole;
import jy.game.components.save.JyGameSave;
import jy.game.components.shop.JyShop;
import jy.game.components.story.JyStory;
import jy.game.components.task.JyTask;
import jy.game.components.tower.JyTower;
import jy.game.components.wuxue.pojo.JyAoyi;
import jy.game.components.wuxue.pojo.JyInternalSkill;
import jy.game.components.wuxue.pojo.JySkill;
import jy.game.components.wuxue.pojo.JySpecialSkill;
import jy.game.components.wuxue.pojo.JyUniqueSkill;
import wx.xml.simpleframework.xml.ElementList;
import wx.xml.simpleframework.xml.ElementListUnion;
import wx.xml.simpleframework.xml.Root;
import wx.xml.simpleframework.xml.Serializer;
import wx.xml.simpleframework.xml.core.Persister;

@Root(name = "root")
public class BasePojoManager<T extends BasePojo> {
    @ElementListUnion({
        @ElementList(entry = "battle", inline = true, type = JyBattle.class),
        @ElementList(entry = "story", inline = true, type = JyStory.class),
        @ElementList(entry = "task", inline = true, type = JyTask.class),
        @ElementList(entry = "tower", inline = true, type = JyTower.class),
        @ElementList(entry = "aoyi", inline = true, type = JyAoyi.class),
        @ElementList(entry = "internal_skill", inline = true, type = JyInternalSkill.class),
        @ElementList(entry = "skill", inline = true, type = JySkill.class),
        @ElementList(entry = "special_skill", inline = true, type = JySpecialSkill.class),
        @ElementList(entry = "unique", inline = true, type = JyUniqueSkill.class),
        @ElementList(entry = "item", inline = true, type = JyItem.class),
        @ElementList(entry = "map", inline = true, type = JyMap.class),
        @ElementList(entry = "menpai", inline = true, type = JyMenpai.class),
        @ElementList(entry = "mod", inline = true, type = JyModInfo.class),
        @ElementList(entry = "music", inline = true, type = JyMusics.class),
        @ElementList(entry = "role", inline = true, type = JyRole.class),
        @ElementList(entry = "gamesave", inline = true, type = JyGameSave.class),
        @ElementList(entry = "trigger", inline = true, type = JyGlobalTrigger.class),
        @ElementList(entry = "resource", inline = true, type = JyResource.class),
        @ElementList(entry = "user_defined_animations", inline = true, type = UserDefinedAnimationsData.class),
        @ElementList(entry = "shop", inline = true, type = JyShop.class)
    })
    public @ElementList(inline = true) List<T> datas   = new ArrayList<>();
    public                             String  message = "";

    @NonNull
    public static <T extends BasePojo> BasePojoManager<T> build(String xml, Class<T> classType) {
        return build(new File(xml), classType);
    }

    @NonNull
    public static <T extends BasePojo> BasePojoManager<T> build(File file, Class<T> classType) {
        try {
            Serializer         serializer = new Persister();
            BasePojoManager<T> root       = serializer.read(BasePojoManager.class, file, false);
            return root;
        } catch (Exception e) {
            e.printStackTrace();

            BasePojoManager<T> root = new BasePojoManager<>();
            root.message = e.getMessage();
            return root;
        }
    }

    public boolean success() {
        return TextUtils.isEmpty(message);
    }
}